<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<!-- This file is required to reside on alfresco.com, it provides a user (with admin 
      rights on their local Alfresco install) with a method to authenticate the 
		publishing services with an account on an external publisher (e.g. Facebook)
		This feature is triggered inside the Admin Console Channel Tools page of Share.
		This page is the target of the redirect URL specified during app registration by
		Alfresco when generating the App Key.
-->
	<head>
		<title>Alfresco &raquo; Authorisation</title>
		<style type="text/css">
			body {margin:3em;font-family:arial,helvetica,clean,sans-serif;}
			div.header {background:#56A3D9;}
			h1 {color: white;font-size: 1.3em;padding:5px 6px 3px;}
		</style>
	</head>
	<body>
		<img src="http://www.alfresco.com/images/alfresco-logo.png" alt="Alfresco" />
		<div class="header"><h1>Publishing Channel Authorisation</h1></div>
		<p id="status">Completing your authorisation...</p>
		<script>
         // The Auth token is received from the publisher as a hash on the URL of this call
         // This needs passing back to the originating Alfresco instance.
         // One of the easiest ways to achieve this is if we still have a handle to the
         // window that opened the page:
         var statusEl = document.getElementById("status");
         try
         {
            // We have a handle on the window:
            window.opener.location.hash = window.location.hash;
            statusEl.innerHTML = "Your authorisation has been completed. Please return to Alfresco Share.";
            self.close();
         }
         catch (err)
         {
            // Errors here are likely to be caused by a "Permission Denied" on the window.opener.location (or window.opener doesn't exist). Either way:
            // if we have the state param on the hash, use that, it should contain the authCallbackUrl.
            var hashArray = window.location.hash.split("state="),
               authCallbackUrl = (hashArray[1])? hashArray[1].substring(0, hashArray[1].indexOf("&")): false ;
            if(authCallbackUrl)
            {
               // The Alfresco Auth Return webscript expects the access token as a query string parameter, not as a URL fragment.
               var accessToken = window.location.hash.split("access_token="),
                  url = decodeURIComponent(authCallbackUrl) + "?access_token=" + accessToken[1];
               statusEl.innerHTML = "Your authorisation has been received.<br /> You are being redirected back to Alfresco Share to finish processing the authorisation."
               // Use a client side redirect to send the user there. That page should sort everything out and close itself, if possible.
               window.location = url;
            }
            // else, there's nothing we can do:
            else
            {
               statusEl.innerHTML = "Your authorisation could not be completed. Please return to the Alfresco Share Admin Console and try again."
            }
         }
		</script>
	</body>
</html>